import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Modal, notification} from "antd";
import FormEditor from '../../components/FormEditor';
import {hideEdit, modify, query} from "./actions";

class ModifyModal extends PureComponent {
    handleModify = () => {
        const formEditor = this.formEditor.props.form;
        if (formEditor) {
            const {dispatch, model, filter} = this.props;
            formEditor.validateFieldsAndScroll((err, values) => {
                if (!err) {
                    const updateModel = {...model, ...values};
                    dispatch(modify({...updateModel})).then(action => {
                        if (action.error !== true) {
                            dispatch(hideEdit());
                            dispatch(query({
                                ...filter
                            }));
                            notification.success({
                                message: '修改成功'
                            });
                        }
                    });
                }
            })
        }
    };
    handleCancel = () => {
        const {dispatch} = this.props;
        dispatch(hideEdit());
    };


    render() {
        const {loading, visible, model} = this.props;
        const schema = [
            {
                title: '编号信息',
                fields: [
                    {
                        field: 'jcNo',
                        title: '进仓编号',
                        readonly: true
                    }, {
                        field: 'orderNo',
                        title: '订单编号',
                        readonly: model.orderLock
                    }, {
                        field: 'line',
                        title: '线路',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '货物信息',
                fields: [
                    {
                        field: 'pack',
                        title: '包装',
                        readonly: model.orderLock
                    }, {
                        field: 'packages',
                        title: '件数',
                        readonly: model.orderLock
                    }, {
                        field: 'weight',
                        title: '重量',
                        readonly: model.orderLock
                    }, {
                        field: 'volume',
                        title: '方数',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '进度信息',
                fields: [
                    {
                        field: 'process',
                        title: '进度',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '客户资料',
                fields: [
                    {
                        field: 'jcOrder',
                        title: '进仓单',
                        readonly: model.orderLock
                    }, {
                        field: 'bgFile',
                        title: '报关资料',
                        readonly: model.orderLock
                    }, {
                        field: 'carFile',
                        title: '随车资料',
                        readonly: model.orderLock
                    }, {
                        field: 'gng',
                        title: 'GNG码',
                        readonly: model.orderLock
                    }, {
                        field: 'pName',
                        title: '品名导入表',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '进区关务',
                fields: [
                    {
                        field: 'bgyld',
                        title: '报关预录单',
                        readonly: model.orderLock
                    }, {
                        field: 'carInfo1',
                        title: '进区申报绑卡/车辆信息',
                        readonly: model.orderLock
                    }, {
                        field: 'deliveryTime',
                        title: '送货时间',
                        readonly: model.orderLock
                    }, {
                        field: 'carGroup',
                        title: '车队',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '装箱及关务',
                fields: [
                    {
                        field: 'packageTime',
                        title: '装箱日期',
                        readonly: model.orderLock
                    }, {
                        field: 'carInfo2',
                        title: '空柜绑卡进区/车辆信息',
                        readonly: model.orderLock
                    }, {
                        field: 'carInfo3',
                        title: '核放绑卡出区/车辆信息',
                        readonly: model.orderLock
                    }, {
                        field: 'packageNo',
                        title: '箱号',
                        readonly: model.orderLock
                    }, {
                        field: 'blInfo',
                        title: '班列信息',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '出口报关',
                fields: [
                    {
                        field: 'ckbg',
                        title: '出口报关',
                        readonly: model.orderLock
                    }, {
                        field: 'bzg',
                        title: '报转关',
                        readonly: model.orderLock
                    }, {
                        field: 'unLock',
                        title: '解锁',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '仓库进出库',
                fields: [
                    {
                        field: 'sign',
                        title: '入库签收',
                        readonly: model.orderLock
                    }, {
                        field: 'jg',
                        title: '加固材料',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '箱子及费用',
                fields: [
                    {
                        field: 'belong',
                        title: '箱属',
                        readonly: model.orderLock
                    }, {
                        field: 'dcTime',
                        title: '堆存时间',
                        readonly: model.orderLock
                    }, {
                        field: 'dcFee',
                        title: '堆存费用',
                        readonly: model.orderLock
                    }, {
                        field: 'pNo',
                        title: '放箱单号',
                        readonly: model.orderLock
                    }, {
                        field: 'pickFee',
                        title: '提空费',
                        readonly: model.orderLock
                    }, {
                        field: 'sd',
                        title: '水单',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '杂费',
                fields: [
                    {
                        field: 'ticketCount',
                        title: '票数',
                        readonly: model.orderLock
                    }, {
                        field: 'xy',
                        title: '续页',
                        readonly: model.orderLock
                    }, {
                        field: 'kz',
                        title: '核注',
                        readonly: model.orderLock
                    }, {
                        field: 'zgld',
                        title: '整柜联单及续页',
                        readonly: model.orderLock
                    }, {
                        field: 'yc',
                        title: '压车',
                        readonly: model.orderLock
                    }, {
                        field: 'other',
                        title: '其他',
                        readonly: model.orderLock
                    }
                ]
            }, {
                title: '备注信息',
                fields: [
                    {
                        field: 'remark',
                        title: '备注',
                        readonly: model.orderLock
                    }
                ]
            }];
        return (
            <Modal
                title="修改"
                visible={visible}
                onOk={this.handleModify}
                onCancel={this.handleCancel}
                confirmLoading={loading}
                okText="保存"
                cancelText="取消"
                width="80%"
            >
                <FormEditor
                    schema={schema}
                    column={3}
                    defaultReadonly={false}
                    showActionBar={false}
                    defaultValues={model}
                    wrappedComponentRef={inst => (this.formEditor = inst)}
                />
            </Modal>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.consolidate.modify
    };
};


export default connect(mapStateToProps)(ModifyModal);
